home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Enhancements / Awari / ARexx / Awari.dopus5
Encoding:
Text File  |  1997-12-31  |  8.8 KB  |  353 lines

  1. /* $VER: Awari.dopus5 1.1 (7.6.98) by D.Clarke
  2.  
  3.    The FIRST game in a DOpus lister!
  4.  
  5.                                  A  W  A  R  I
  6.                                ~~~~~~~~~~~~~~~~~
  7.  
  8.                                    My Side
  9.  
  10.                            | 6 | 5 | 4 | 3 | 2 | 1 |
  11.                            |ooo|ooo|ooo|ooo|ooo|ooo|
  12.                   My home  |   |   |   |   |   |   | Your home
  13.                            |ooo|ooo|ooo|ooo|ooo|ooo|
  14.                            | 1 | 2 | 3 | 4 | 5 | 6 |
  15.  
  16.                                  Your  Side
  17.  
  18.              Awari is an African game played with seven sticks and
  19.           thirty-six stones or beans laid out as shown above.  The
  20.           board is divided into six compartments or pits on each side.
  21.           In addition, there are two special HOME pits at the end.
  22.  
  23.              A move is made by taking all the beans from any
  24.           (non-empty) pit on your own side.  Starting from the pit to
  25.           the right of this one, these beans are "sown" one in each
  26.           pit working around the board anticlockwise.
  27.  
  28.              A turn consists of one or two moves.  If the last bean of
  29.           your move is sown in your own home you may take a second
  30.           move.
  31.  
  32.              If the last bean sown in a move lands in an empty pit,
  33.           provided that the opposite pit is not empty, all the beans
  34.           in the opposite pit, together with the last bean sown are
  35.           "captured" and moved to the players home.
  36.  
  37.              When either side is empty, the game is finished. The
  38.           player with the most beans in his home has won.
  39.  
  40.              In the computer version, the board is printed as 14
  41.           numbers representing the 14 pits.
  42.  
  43.                             3   3   3   3   3   3
  44.                          0                         0
  45.                             3   3   3   3   3   3
  46.  
  47.              The pits on your (lower) side are numbered 1-6 from left
  48.           to right.  The pits on my side (computer) are numbered from
  49.           my left (your right).
  50.  
  51.              When it is your move, the Status bar will display
  52.           "Your Move:".  To make a move you select a number from the
  53.           toolbar, if the move is valid the board will be updated.
  54.  
  55.              If the last bean lands in your home the computer shows
  56.           "Again:" in the Status bar and you can select another move.
  57.  
  58.              The computer move is shown below the board, which will be
  59.           shown in it's new state.  If the computer was able to make a
  60.           second move, it will be shown as 'x,y'.  The computer always
  61.           offers you the first move.  This is considered to be a slight
  62.           advantage.
  63.  
  64.              There is a learning mechanism in the program that causes
  65.           the play of the computer to improve as it plays more games.
  66.  
  67. */
  68. options results
  69. address 'DOPUS.1'
  70. if ~show('L','rexxsupport.library') then call addlib('rexxsupport.library',0,-30)
  71. dopus version
  72. version = result ~= 'RESULT' & translate(result,'.',' ') >= 5.1538
  73. if ~version then do
  74.   dopus request '"Sorry, requires V5.7+ of DOpus" OK'
  75.   exit
  76.   end
  77. alltraps = 'Awari1 Awari2 Awari3 Awari4 Awari5 Awari6 AwariHelp AwariExit'
  78. plural. = 's'
  79. plural.1 = ''
  80. lf = '0a'x
  81. n = 0
  82. computer = 0
  83. human = 0
  84.  
  85. call openport('awari-handler')
  86. lister new '258/353/240/120'
  87. AHandle = result
  88. lister set AHandle busy on
  89. lister set AHandle display name
  90. lister set AHandle sort name
  91. lister set AHandle handler 'awari-handler'
  92. lister set AHandle toolbar 'Awari.tb'
  93. lister set AHandle title 'DOpus Awari'
  94. traps = alltraps
  95. do while traps ~= ''
  96.   parse var traps trapcommand traps
  97.   dopus command '"'trapcommand'"' temp private handler
  98.   dopus addtrap '"'trapcommand'"' 'awari-handler'
  99. end
  100. lister set AHandle busy off
  101.  
  102. if open('varfile','DOpus5:System/Awari.var','r') then do
  103.   scores = readln('varfile')
  104.   do until eof('varfile')
  105.     interpret readln('varfile')
  106.   end
  107.   call close('varfile')
  108.   computer = value(strip(left(scores,4)))
  109.   human = value(strip(right(scores,4)))
  110.   end
  111.  
  112. Begin:
  113. lister remove AHandle '4'
  114. e = 0
  115. b. = 3
  116. c = 0
  117. f.n = 0
  118. b.6 = 0
  119. b.13 = 0
  120.  
  121. do forever
  122.   call PrintIt
  123.   lister set AHandle header 'Your Move:'
  124.   lister refresh AHandle full
  125.   call GetHumanInput
  126.   call MakeMove
  127.   call PrintIt
  128.   if ~e then leave
  129.   if m = 6 then do
  130.     lister set AHandle header 'Again:'
  131.     lister refresh AHandle full
  132.     call GetHumanInput
  133.     call MakeMove
  134.     call PrintIt
  135.     end
  136.   if ~e then leave
  137.   Move = 'My move is: '
  138.   call CPU_move
  139.   if ~e then leave
  140.   if m = 13 then do
  141.     Move = Move || ','
  142.     call CPU_move
  143.     end
  144.   if ~e then leave
  145.   fileinfo.name = '4'
  146.   fileinfo.display = Move
  147.   fileinfo.type = -4
  148.   lister addstem AHandle fileinfo.
  149.   lister refresh AHandle
  150. end
  151.  
  152. if h = 13 then call PrintIt
  153.  
  154. EndGame = 'Game Over'
  155. d = b.6 - b.13
  156. if d = 0 then EndGame = EndGame||lf||'Drawn game...'
  157. else do
  158.   n = n + 1
  159.   if d > 0 then do
  160.     EndGame = EndGame||lf||'You win by 'd' point'plural.d
  161.     human = human + 1
  162.     end
  163.   else do
  164.     d = abs(d)
  165.     EndGame = EndGame||lf||'I win by 'd' point'plural.d
  166.     computer = computer + 1
  167.     end
  168.   end
  169. lister request AHandle '"'EndGame'" OK'
  170. signal Begin
  171.  
  172. MakeMove:
  173. k = m
  174. call Moveit m
  175. e = 0
  176. if k > 6 then k = k - 7
  177. c = c + 1
  178. if c < 9 then f.n = f.n * 6 + k
  179. do i = 7 to 12
  180.   if b.i ~= 0 then do
  181.     do j = 0 to 5
  182.       if b.j ~= 0 then do
  183.         e = 1
  184.         return
  185.         end
  186.     end
  187.     end
  188. end
  189. return
  190.  
  191. Moveit: procedure expose b. h m
  192. parse arg m
  193. p = b.m
  194. b.m = 0
  195. do p
  196.   m = m + 1
  197.   if m > 13 then m = m - 14
  198.   b.m = b.m + 1
  199. end
  200. p = 12 - m
  201. if b.m = 1 & m ~= 6 & m ~= 13 & b.p ~= 0 then do
  202.   b.h = b.h + b.p + 1
  203.   b.m = 0
  204.   b.p = 0
  205.   end
  206. return
  207.  
  208. CPU_Move:
  209. h = 13
  210. d = -99
  211. q = -99
  212. do i = 0 to 13
  213.   g.i = b.i
  214. end
  215.  
  216. do j = 7 to 12
  217.   if b.j = 0 then iterate
  218.   g = 0
  219.   call Moveit j
  220.   do i = 0 to 5
  221.     if b.i = 0 then iterate
  222.     l = b.i + i
  223.     r = (l > 13)
  224.     l = l // 14
  225.     p = 12 - l
  226.     if b.l =  0 & l ~= 6 & l ~= 13 then r = b.p + r
  227.     if r > q then q = r
  228.   end
  229.   q = b.13 - b.6 - q
  230.   if c < 9 then do
  231.     k = j
  232.     if k > 6 then k = k - 7
  233.     do i = 0 to n - 1
  234.       if f.n * 6 + k  =  trunc(f.i / 6 ** (7 - c) + 0.1) then q = q - 2
  235.     end
  236.     end
  237.   do i = 0 to 13
  238.     b.i = g.i
  239.   end
  240.   if q >= d then do
  241.     a = j
  242.     d = q
  243.     end
  244. end
  245.  
  246. m = a
  247. Move = Move (m - 6)
  248. call MakeMove
  249. return
  250.  
  251. PrintIt:
  252. p = '    '
  253. do i = 12 to 7 by -1
  254.   p = p||Format(b.i,4,' ')
  255. end
  256. fileinfo.name = 1
  257. fileinfo.display = p
  258. fileinfo.type = -1
  259. lister addstem AHandle fileinfo.
  260. fileinfo.name = 2
  261. fileinfo.display = '  '||Format(b.13,2,' ')||'                          '||Format(b.6,2,' ')
  262. fileinfo.type = -2
  263. lister addstem AHandle fileinfo.
  264. p = '    '
  265. do i = 0 to 5
  266.   p = p||Format(b.i,4,' ')
  267. end
  268. fileinfo.name = 3
  269. fileinfo.display = p
  270. fileinfo.type = -1
  271. lister addstem AHandle fileinfo.
  272. lister refresh AHandle
  273. return
  274.  
  275. GetHumanInput:
  276. valid = 0
  277. do until valid
  278.   if waitpkt('awari-handler') then do
  279.     packet = getpkt('awari-handler')
  280.     if packet ~= '00000000'x then do
  281.       event = strip(getarg(packet,0))
  282.       if event = 'inactive' then signal TheExit
  283.       if event = 'AwariExit' then signal TheExit
  284.       if event = 'AwariHelp' then call Help
  285.       if pos(event,alltraps) = 0 then do
  286.         text = 'Unsupported command, please use'||lf||'the toolbar for input.'
  287.         lister request AHandle '"'text'" OK'
  288.         valid = 0
  289.         end
  290.       else if event ~= 'AwariHelp' then do
  291.         m = value(right(event,1)) - 1
  292.         if b.m = 0 then do
  293.           text = 'Illegal move: 'm + 1' is empty.'
  294.           lister request AHandle '"'text'" OK'
  295.           valid = 0
  296.           end
  297.         else valid = 1
  298.         end
  299.       end
  300.       h = 6
  301.       call reply(packet,0)
  302.     end
  303. end
  304. return
  305.  
  306. Format: procedure
  307. parse arg zz,num,str
  308. pp = reverse(overlay(reverse(trunc(zz + .5)),copies(str,num)))
  309. return pp
  310.  
  311. Help:
  312. if ~open('helpfile','T:Awari.help','w') then do
  313.   text = "Error: Unable to create help file"||lf||"''T:Awari.help''"||lf||"You''re on your own :)"
  314.   lister request AHandle '"'text'" OK'
  315.   return
  316.   end
  317. else do
  318.   do Line = 5 by 1
  319.     if left(Sourceline(Line),2) = '*/' then do
  320.       call close('helpfile')
  321.       dopus read 'T:Awari.help'
  322.       readhandle = result
  323.       return
  324.       end
  325.   call writeln('helpfile',Sourceline(Line))
  326.   end
  327.   end
  328.  
  329. TheExit:
  330. text = 'Thanks for playing'||lf||'DOpus Awari.'
  331. text = text||lf||lf||'Current Scores:'||lf||'Computer: 'computer||lf||'Human:    'human||lf
  332. dopus request '"'text'" OK'
  333. if open('varfile','DOpus5:System/Awari.var','w') then do
  334.   scores = computer||'    '||human
  335.   call writeln('varfile',scores)
  336.   call writeln('varfile','n = 'n)
  337.   do i = 0 to n
  338.     call writeln('varfile', 'f.'i' = "'f.i'"')
  339.   end
  340.   call close('varfile')
  341.   call delete('T:Awari.help')
  342.   end
  343.   traps = alltraps
  344.   do while traps ~= ''
  345.     parse var traps trapcommand traps
  346.     dopus remtrap '"'trapcommand'"' 'awari-handler'
  347.     dopus command '"'trapcommand'"' temp remove
  348.   end
  349.   call closeport('awari-handler')
  350.   if event ~= 'inactive' then lister close AHandle
  351.   if readhandle ~= 0 then dopus read readhandle quit
  352.   exit
  353.